package com.example.wx.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.wx.domain.CompleteCase;
import com.example.wx.domain.SingleBriefCase;
import com.example.wx.persistence.CaseAnalysisDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class CaseAnalService {

    @Autowired
    private CaseAnalysisDAO caseAnalysisDAO;

    public JSONObject getAnalList(){

        //获取data列表
        ArrayList<SingleBriefCase> dataArray = caseAnalysisDAO.getAllCase();
        JSONArray dataList = JSONArray.parseArray(JSON.toJSONString(dataArray));

        String code = "-1";
        String msg = "";

        if(dataList!=null){
            msg = "data in DataBase has been queried";
            code = "0";
        } else {
            msg = "Nothing in DataBase";
        }

        //总的返回json：包括code、msg、data数组
        JSONObject res = new JSONObject();
        res.put("code", code);
        res.put("msg", msg);
        JSONObject data = new JSONObject();
        data.put("all_case", dataList);
        res.put("data", data);

        return res;
    }

    public JSONObject getUncheckedList(){

        ArrayList<CompleteCase> dataArray = caseAnalysisDAO.getUncheckedCase();
        JSONArray dataList = JSONArray.parseArray(JSON.toJSONString(dataArray));

        String code = "-1";
        String msg = "";

        if(dataList!=null){
            msg = "CompleteCases in DataBase has been queried";
            code = "0";
        } else {
            msg = "Nothing in DataBase";
        }

        //总的返回json：包括code、msg、data数组
        JSONObject res = new JSONObject();
        res.put("code", code);
        res.put("msg", msg);
        JSONObject data = new JSONObject();
        data.put("all_case", dataList);
        res.put("data", data);

        return res;
    }

    public JSONObject getUnckCaseById(String case_id){
        ArrayList<CompleteCase> dataArray = caseAnalysisDAO.getUncheckedCase();
        JSONObject res = new JSONObject();
        String code = "-1";
        String msg = "not have case_id -- complete case";

        for (int i=0;i<dataArray.size();i++) {
            if (dataArray.get(i).getCase_id().equals(case_id)) {
                code = "0";
                msg = "has get case_id -- complete case";
                res.put("code", code);
                res.put("msg", msg);
                res.put("data", dataArray.get(i));
                return res;
            }
        }

        res.put("code", code);
        res.put("msg", msg);
        res.put("data", "no data");
        return res;
    }

}
